package com.jiangyg.mall.core;

import com.jiangyg.mall.core.support.env.EnvConstant;
import com.jiangyg.mall.core.support.web.RestfulExceptionHandler;
import com.jiangyg.mall.core.support.web.WebI18nHandlerInterceptor;
import com.jiangyg.mall.core.utils.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * 类描述：SpringMvc配置
 * <p>引用通用的restful异常处理器</p>
 *
 * @author jiangyg
 * @version 2.0
 * @date 2019-10-14
 */
@Slf4j
@Import(RestfulExceptionHandler.class)
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class WebAutoConfiguration implements WebMvcConfigurer {

    /**
     * 内容裁决解析器
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        // 不检查Accept请求头
        configurer.ignoreAcceptHeader(true);
        configurer.parameterName("mediaType");
        // 请求以.html结尾的会被当成MediaType.TEXT_HTML
        configurer.mediaType("html", MediaType.TEXT_HTML);
        // 设置默认的media type
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
        // 请求以.json或者.do结尾的会被当成MediaType.APPLICATION_JSON
        // configurer.mediaType("do", MediaType.APPLICATION_JSON);
        // configurer.mediaType("json", MediaType.APPLICATION_JSON);
    }

    /**
     * 功能描述：添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 追加国际化处理
        registry.addInterceptor(new WebI18nHandlerInterceptor());
    }

    /**
     * 功能描述：静态资源处理
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/static/");
    }

    /**
     * 功能描述：视图跳转控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 设置默认首页
        registry.addViewController("/");
        // registry.setViewName("forward:index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    /**
     * 功能描述：配置视图解析器
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/views/");
        viewResolver.setSuffix(".html");
        viewResolver.setOrder(0);
        viewResolver.setContentType("text/html;charset=UTF-8");
        registry.viewResolver(viewResolver);
    }

    /**
     * 功能描述：请求参数校验错误信息
     *
     * @return LocalValidatorFactoryBean
     */
    @Bean
    public LocalValidatorFactoryBean buildLocalValidatorFactoryBean() {
        String basename = EnvConstant.getString("spring.validation.basename");
        Logger.info(log, () -> String.format("[项目启动]-请求参数校验提示国际化路径：%s", basename));
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        ResourceBundleMessageSource configurer = new ResourceBundleMessageSource();
        configurer.setBasename(basename);
        validator.setValidationMessageSource(configurer);
        return validator;
    }

    /**
     * 功能描述：跨域实现
     *
     * @param registry 注册器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

}
